package com.api.gbhg.zengping.common;

import com.api.gbhg.liuwenbo.utils.FormatUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetData;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: ecology_zp
 * @author: Mr.zeng
 * @create: 2020-12-23 10:07
 *
 * 通用查询
 **/
public class CommonDao {
    public static final Logger LOGGER = LoggerFactory.getLogger("gbhg");

    /**
     * 根据请求id查询主表信息
     * @param rs
     * @param tableName
     * @param requestId
     * @return
     */
    public static RecordSet selectByRequestId(RecordSet rs, String tableName, String requestId){
        String sql="SELECT * FROM  "+tableName+" a left join workflow_requestbase b on a.requestid=b.requestid WHERE a.requestId='"+requestId+"'";
        rs.executeQuery(sql);
        LOGGER.info("根据请求id查询主表信息：---------->"+sql);
        return rs;
    }

    /**
     * 根据表名称主表id 查询明细信息
     * @param detailTableName
     * @param mainId
     * @return
     */
    public static RecordSet selectByDetailTable(String detailTableName,String mainId){
        RecordSet rs=new RecordSet();
        String sql="select * from "+detailTableName+"  where mainid=?";
        rs.executeQuery(sql,mainId);
        LOGGER.info("根据主表id查询明细表：---------->"+sql);
        return rs;
    }

    /**
     * 返回流程明细表数组
     * @param requestInfo
     * @return
     */
    public static DetailTable[] getDetailTables(RequestInfo requestInfo){
        return requestInfo.getDetailTableInfo().getDetailTable();
    }

    public static RecordSet selectJingfei(String lcjlid){
        RecordSet rs=new RecordSet();
        String sql="select * from uf_jingfei where lcjlid="+lcjlid+"";
        rs.executeQuery(sql);
        return rs;
    }


    /**
     * 选择框（字符串）转换成显示值(数值)
     * @param fieldname
     * @param fieldValue
     * @param detailTable
     * @return
     */
    public static Integer getSelectionBox(String fieldname,String fieldValue,String detailTable){
        RecordSet rs = new RecordSet();
        String sql = "select wf_si.selectvalue,wf_si.selectname from workflow_billfield wf_bf " +
                "right join workflow_selectitem wf_si on wf_bf.id = wf_si.fieldid  " +
                "where selectname='"+fieldValue+"' and  wf_bf.fieldname ='"+fieldname+"'";
        if(detailTable!=null){
            StringBuffer sb=new StringBuffer();
            sb.append(sql);
           sb.append(" and wf_bf.detailtable = "+detailTable+"");
           sql=sb.toString();
        }
        rs.executeQuery(sql);
        while (rs.next()){
            if (rs.getString("selectname").equals( fieldValue)){
                return rs.getInt("selectvalue");
            }
        }
        return null;
    }

    /**
     * 获取表数据
     * @param flowTableName
     * @param requestId
     * @return
     */
    public Map<String,Object> getMainTabData(String flowTableName, String requestId){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select * from "+flowTableName+" where requestId = "+requestId+" ");
        RecordSetData data = rs.getData();
        rs.next();
        Map<String,Object> map = new HashMap<>();

        String[] columnName = data.getColumnName();
        int[] columnType = data.getColumnType();
        for (int i = 0; i < columnName.length ; i++) {
           if(columnType[i] == 3){
                map.put(columnName[i], FormatUtil.DmFormat(rs.getDouble(columnName[i])));
            }else{
                map.put(columnName[i],rs.getString(columnName[i]));
            }
        }
        List<String>[] lists = new ArrayList[5];
        int[] a = new int[2];
        a[0] = 1;
        a[1] = 2;
        return map;
    }

    /**
     * 过滤一遍-处于预算审核的流程id
     * @param requestId
     * @return
     */
    public String getYSHPRequestId(String requestId){
        RecordSet rs=new RecordSet();
        String sql="select * from workflow_requestbase r\n" +
                "  left join workflow_nodebase  n on r.currentnodeid=n.id\n" +
                "  where r.requestid in("+requestId+")";
        rs.executeQuery(sql);
        StringBuffer sb=new StringBuffer();
        while (rs.next()){
            if(rs.getString("nodename").equals("预算核批") || rs.getString("nodename").equals("经费二科核批") ||  rs.getString("nodename").equals("预算科核批")){
                sb.append(rs.getString("requestid"));
                sb.append(",");
            }
        }
        String str = sb.toString();
        if(str.equals("")){
            return "";
        }
        return str.substring(0,str.lastIndexOf(","));
    }

    /**
     * 全局分页方法
     * @param pageNum
     * @param pageSize
     * @param orderId 排序字段
     * @return
     */
    public static String limitSql(Object pageNum,Object pageSize,String orderId){
        RecordSet rts=new RecordSet();
        StringBuffer sb=new StringBuffer();
        if("sqlserver".equals(rts.getDBType())){
            sb.append(" order by "+orderId+" offset "+pageNum+" rows fetch next "+pageSize+" rows only");
        }else if("mysql".equals(rts.getDBType())){
            sb.append(" LIMIT "+pageNum+","+pageSize+"");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        StringBuffer sb=new StringBuffer();
        sb.append("123456");
        sb.append(",");
        sb.append("123456");
        sb.append(",");
        sb.append("123456");
        sb.append(",");
        System.out.println(sb.toString().substring(0,sb.toString().lastIndexOf(",")));

    }
}
